var express = require("express");
var router = express.Router();
let { listModel, userModel, cartModel } = require("../db/index");
const alipaySdk = require("../alipay");
let multiparty = require("multiparty");
const AlipayFormData = require("alipay-sdk/lib/form").default;

//支付宝支付接口
router.post("/pay", async (req, res) => {
  try {
    let { orderId, price, name } = req.body;
    const formData = new AlipayFormData();
    formData.setMethod("get");
    formData.addField("bizContent", {
      out_trade_no: orderId,
      product_code: "FAST_INSTANT_TRADE_PAY",
      total_amount: price,
      subject: name,
      body: "商品详情",
    });
    formData.addField("returnUrl", "http://localhost:5173/my");

    const paymentUrl = await alipaySdk.exec(
      "alipay.trade.wap.pay",
      {},
      { formData }
    );
    res.send({
      code: 200,
      success: true,
      msg: "支付中",
      paymentUrl,
    });
  } catch (error) {
    res.status(500).send({
      code: 500,
      success: false,
      msg: "支付接口异常",
      error: error.message,
    });
  }
});

/* GET users listing. */
router.post("/", async function (req, res, next) {
  let cart_id = req.body.cart_id;
  console.log(cart_id);
  let carts = await cartModel.find();
  let price = 0;
  for (let i = 0; i < carts.length; i++) {
    if (cart_id.find((i) => i == carts[i].cart_id)) {
      price += carts[i].price;
    }
  }
  res.send({ code: 200, price });
});
//发布商品
router.post("/add", async (req, res) => {
  try {
    await listModel.create(req.body);
    res.send({ code: 200, msg: "发布成功" });
  } catch (err) {
    res.send({ code: 400, msg: "发布失败" });
  }
});
router.post("/up", async (req, res) => {
  var form = new multiparty.Form(); //实例一个multiparty对象
  form.uploadDir = "public";
  form.parse(req, function (err, fields, files) {
    let path = files.file[0].path;
    res.send({
      code: 200,
      url: "http://localhost:3000/" + path,
    });
  });
});
//发布商品
router.post("/Fabu", async (req, res) => {
  try {
    await listModel.create(req.body);
    res.send({
      code: 200,
    });
  } catch (err) {
    res.send({ code: 400, err });
  }
});
//返回购物车数据
router.get("/cart", async (req, res) => {
  let user_id = req.query.user;
  let data = await cartModel.find().populate("cart_id");
  let info = data.filter((i) => i.user_id == user_id);
  let userList = await userModel.find();

  res.send({ code: 200, info, userList });
});
//返回支付商品数据
router.get("/pro", async (req, res) => {
  let pros = req.query.pros;
  pros = pros.split(",");
  let data = await listModel.find();
  let info = await data.filter((i) => pros.find((j) => j == i._id));
  // console.log(info);
  res.send({ code: 200, info });
});
//购物车数据修改状态
router.post("/cahngecart", async (req, res) => {
  let cartid = req.body.cartid;
  let data = await cartModel.find();
  let info = data.filter((i) => cartid.find((j) => j == i.cart_id));
  // console.log(info);
  for (let i = 0; i < info.length; i++) {
    await cartModel.updateOne({ _id: info[i]._id }, { state: 1 });
  }
  res.send({ code: 200, msg: "修改成功" });
});
module.exports = router;
